<?php 
include_once('HttpClient.class.php'); 
class db{
	function Connect ($host,$user,$pass)
	{

			$con = mysql_connect ($host,$user,$pass);
			$bool = mysql_select_db("ip",$con);
			if ($bool == 0)
			{
				echo "Select DB Error";
			}
			if ($con == false)
			{
				echo "Connect Server Error";	
			}
	}
	function Get()
	{
		$sql = "select * from ip limit 1";
		$result = mysql_query($sql);
		if ($result)
		{		
			while($row =mysql_fetch_array($result))
			{
				$sql = "delete from ip where id = " . $row[0];
				$this->ExecSql($sql);
				return $row[1];
			}
			$this->WriteProxy();
		}
		else {
			return $this->Get();
		}
	}
	function ExecSql($sql)
	{
		$result = mysql_query($sql);
		if($result == false)
		{
			echo "Exec Sql Error";	
		}
		return $result;
	}
	function WriteProxy()
	{
		$http = new HttpClient("vip.httpdaili.com");
		$request = $http->quickGet("Http://vip.httpdaili.com/APi.asp?ddbh=47890&old=1&noinfo=true&sl=20");
		if (empty($request))
		{
			//echo "this empty nofind";
			echo false ;
			return;
		}
		
		//echo "this WriteProxy() nofind";
		$startPos = strpos($request,"<textarea id=\"t\">")+17;
		$endPos = strrpos($request,"</textarea>");
		$result = substr($request,$startPos,$endPos-$startPos);
		$arr = array();
		$arr = explode("\r\n",$result);
		foreach ($arr as $ip)
		{
			if(empty($ip))
			{
				continue;	
			}
			$ip = str_replace("<br>","",$ip);
			$sql = "insert into ip (`ip`) values ('". $ip ."')";
			$this->ExecSql($sql);
		}
		
	}
}

?>